<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="案件名:" prop="caseId">
          <el-select v-model="formData.caseId" placeholder="请选择案件名" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in dataSource.caseId" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名:" prop="name">
          <el-input v-model="formData.name" :clearable="true" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="身份证:" prop="idNumber">
          <el-input v-model="formData.idNumber" :clearable="true" placeholder="请输入身份证"/>
        </el-form-item>
        <el-form-item label="户籍地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true" placeholder="请输入户籍地址"/>
        </el-form-item>
        <el-form-item label="年龄:" prop="age">
          <el-input v-model.number="formData.age" :clearable="true" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="民族:" prop="nation">
          <el-select v-model="formData.nation" placeholder="请选择民族" style="width:100%" :clearable="true" >
            <el-option v-for="(item,key) in nationOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="文化程度:" prop="culture">
          <el-select v-model="formData.culture" placeholder="请选择文化程度" style="width:100%" :clearable="true" >
            <el-option v-for="(item,key) in cultureOptions" :key="key" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="归属乡镇:" prop="townCode">
          <el-select v-model="formData.townCode" placeholder="请选择归属乡镇" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话:" prop="phone">
          <el-input v-model="formData.phone" :clearable="true" placeholder="请输入联系电话"/>
        </el-form-item>
        <el-form-item label="金额:" prop="amount">
          <el-input-number v-model="formData.amount" :precision="2" :clearable="true"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {createCasPersons, findCasPersons, getCasPersonsDataSource, updateCasPersons} from '@/api/person/casPersons'
// 自动获取字典
import {getDictFunc} from '@/utils/format'
import {useRoute, useRouter} from "vue-router"
import {ElMessage} from 'element-plus'
import {reactive, ref} from 'vue'

defineOptions({
  name: 'CasPersonsForm'
})


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const townOptions = ref([])
const cultureOptions = ref([])
const nationOptions = ref([])
const formData = ref({
  caseId: undefined,
  name: '',
  idNumber: '',
  address: '',
  age: undefined,
  nation: '',
  culture: '',
  townCode: '',
  phone: '',
  amount: 0,
})
// 验证规则
const rule = reactive({
  caseId: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  name: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  idNumber: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  address: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  age: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  nation: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  culture: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  townCode: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  phone: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  amount: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
})

const elFormRef = ref()
const dataSource = ref([])
const getDataSourceFunc = async () => {
  const res = await getCasPersonsDataSource()
  if (res.code === 0) {
    dataSource.value = res.data
  }
}
getDataSourceFunc()

// 初始化方法
const init = async () => {
  // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
  if (route.query.id) {
    const res = await findCasPersons({ID: route.query.id})
    if (res.code === 0) {
      formData.value = res.data
      type.value = 'update'
    }
  } else {
    type.value = 'create'
  }
  townOptions.value = await getDictFunc('town')
  cultureOptions.value = await getDictFunc('culture')
  nationOptions.value = await getDictFunc('nation')
}

init()
// 保存按钮
const save = async () => {
  btnLoading.value = true
  elFormRef.value?.validate(async (valid) => {
    if (!valid) return btnLoading.value = false
    let res
    switch (type.value) {
      case 'create':
        res = await createCasPersons(formData.value)
        break
      case 'update':
        res = await updateCasPersons(formData.value)
        break
      default:
        res = await createCasPersons(formData.value)
        break
    }
    btnLoading.value = false
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '创建/更改成功'
      })
    }
  })
}

// 返回按钮
const back = () => {
  router.go(-1)
}

</script>

<style>
</style>
